package com.zjweu.waterlevelprediction.controller;

import com.zjweu.waterlevelprediction.mapper.AddressMapper;
import com.zjweu.waterlevelprediction.pojo.*;
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class Test {
    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/getAddress")
    @ResponseBody
    public Object getAddress() {
        List<String> sss = addressMapper.getAddress();
        List<Sss> SSS = new ArrayList<>();
        for (int i = 0; i < sss.size(); i++) {
            Sss sss1 = new Sss();
            sss1.setLabel(sss.get(i));
            sss1.setValue(sss.get(i));
            List<String> ssx = addressMapper.getSsxBySss(sss.get(i));
            List<Ssx> SSX = new ArrayList<>();
            for (int j = 0; j < ssx.size(); j++) {
                Ssx ssx1 = new Ssx();
                ssx1.setLabel(ssx.get(j));
                ssx1.setValue(ssx.get(j));
                SSX.add(ssx1);
            }
            sss1.setChildren(SSX);
            SSS.add(sss1);
        }
        return SSS;
    }

    @RequestMapping("/selectAddressBySSX")
    @ResponseBody
    public Object selectAddressBySSX(@RequestBody Map<String, List> map) {
        List<String> a = map.get("name");
        System.out.println(a + " 查询：" + a.get(1));
        return addressMapper.selectAddressBySSX(a.get(1));
    }

    @RequestMapping("/dataTest")
    @ResponseBody
    public Object dataTest() throws IOException {
        File file = new File("src/main/resources/csv/fallraw_7041JA26clea.csv");
        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);

        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
        for (CsvRow row : csv.getRows()) {
            //通过头获取对应属性
//            index,Unnamed: 0,times,temperature,humidity,winddirection,windpower,rains,rains7041JA23,waterlevels7041JA23,rains7041JA24,waterlevels7041JA24,waterlevels

        }
        return csv.getRows();
    }

    @RequestMapping("/selectDataByNameAndDate")
    @ResponseBody
    public Object selectDataByNameAndDate(@RequestBody Select select) throws IOException, ParseException {
        Date start = select.getDate().get(0);
        Date end = select.getDate().get(1);

        File file = new File("src/main/resources/csv/fallraw_7041JA26clea.csv");
        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);

        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
        List<CsvRow> list = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        for (CsvRow row : csv.getRows()) {
            String a = row.getField(0);
            Date date = sdf.parse(a);
            if (date.before(end) && date.after(start)) {
                list.add(row);
            }
        }

        return list;
    }

    @RequestMapping("/selectAllHtz")
    @ResponseBody
    public Object selectAllHtz() {
        List<Address> htzs=addressMapper.selectAllHtz();
        for(int i=0;i<htzs.size();i++){
            String zm=htzs.get(i).getZm();
            String htz=htzs.get(i).getHtz();
            htzs.get(i).setName(zm+" 最大水位:"+htz);
        }
        return htzs;
    }


    @RequestMapping("/getChart")
    @ResponseBody
    public Object getChart() throws IOException {
        List<RainWaterLevelsData> list = new ArrayList<>();

        File file = new File("src/main/resources/csv/fallraw_7041JA26clea.csv");
        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);

        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);

        for (CsvRow row : csv.getRows()) {
            RainWaterLevelsData data = new RainWaterLevelsData();
            data.setTime(row.getField(0));
            data.setRains(row.getField(7));
            data.setWaterlevels(row.getField(12));
//            通过头获取对应属性
//            index,Unnamed: 0,times,temperature,humidity,winddirection,windpower,rains,rains7041JA23,waterlevels7041JA23,rains7041JA24,waterlevels7041JA24,waterlevels
            list.add(data);
        }
        return list;
    }

}
